package com.winning.sx.microframework.controller.sysmanage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.base_entity.BaseConstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by nicholasyan on 16/5/18.
 */
@Controller
@RequestMapping(value = "/interface/config")
public class InterfaceConfigController {
    @Autowired
    EasyService easyService;
    @Autowired
    BaseConstService constService;
    private ObjectMapper objectMapper = new ObjectMapper();
  @RequestMapping(value = {"/interfaceConfig/{id}"})
    public ModelAndView enterprise(@PathVariable("id") String id) throws Exception {
        ModelAndView mv = new ModelAndView("server_interface/interfaceConfig");
        mv.addObject("interfaceId", id);
        return mv;
    }
    @RequestMapping(value = {"/", ""})
    public ModelAndView index()throws IOException, CustomException {
        ModelAndView mv = new ModelAndView("server_interface/interfaceConfig");
        List state= constService.queryDropDownList("199");
        mv.addObject("state",state);
        return mv;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryAll/{id}"})
    public AjaxTableResponseMessage query(@PathVariable("id") String id) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        try {
            Map param = new HashMap<String, String>();
            param.put("interfaceId", id);
            DataTable table = easyService.queryList("SYS_InterfaceConfig",param);
            ajaxTableResponseMessage.setDatas(table.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) table.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg(e.getMessage());
        }
        return ajaxTableResponseMessage;
    }
    @ResponseBody
    @RequestMapping("/save")
    public AjaxDataResponseMessage saveAsso(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.save("SYS_InterfaceConfig","id",map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("保存失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxDataResponseMessage delete(String datas) throws CustomException {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("SYS_InterfaceConfig","id",map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("保存失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
}
